package com.tms.admin.handler;

import com.tms.common.entity.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/*-----------------------------------
 *   @Time: 2022/9/5 16:53
 *   @Description: Spring 支持的全局异常处理
-----------------------------------*/
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    // 全局异常
    @ExceptionHandler(Exception.class)
    public ResultVO error(Exception e) {
        e.printStackTrace();
        return ResultVO.error().message("执行了全局异常处理");
    }

    // 特定异常
    @ExceptionHandler(ArithmeticException.class)
    public ResultVO error(ArithmeticException e) {
        e.printStackTrace();
        return ResultVO.error().message("执行了 ArithmeticException 异常处理");
    }

    // 自定义异常
    @ExceptionHandler(TMSException.class)
    public ResultVO error(TMSException e) {
        log.error(e.getCode() + e.getMessage());
        return ResultVO.error().code(e.getCode()).message(e.getMessage());
    }
}